function [filename waveguides] = jcb_waveguide_scan_plotter(pathname)


%% variables
do_plots=true;
dirname='matlab_figures';
voltsperum=0.04;
pixel_time_ms=50;


%% load data
if nargin<1
    pathname='.';
end

[filelist pathname] = jcb_select_files(fullfile(pathname,'*_limits.txt'));
filename = strrep(filelist,'_limits.txt','');
tokens   = regexp(filename,'(?<length>\d*)b(?<width>\d*)_(?<polarization>\w)_?(?<separation>\d*)?u?_scan(?<scan_number>\d*)','names');

lengths=4:4:20;
widths=2:2:10;
polarizations='vh';
sepdiffs=[0 4];
num_scans=5;

alloccell=cell(length(lengths),length(widths),length(polarizations),length(sepdiffs),num_scans);
waveguides=struct('filename', alloccell, 'scan_number', alloccell, ...
                'data', alloccell, 'power', alloccell, ...
                'maxval', alloccell,...
                'length', alloccell, 'width', alloccell, ...
                'polarization', alloccell, 'separation', alloccell, ...
                'xlims', alloccell, 'xstep', alloccell, ...
                'ylims', alloccell, 'ystep', alloccell);
hasdata= false(size(waveguides));

for ii=1:length(filename)
    l=lengths==str2double(tokens{ii}.length);
    w=widths==str2double(tokens{ii}.width);
    pol=polarizations==tokens{ii}.polarization;
    scn = str2double(tokens{ii}.scan_number);
    if isempty(tokens{ii}.separation)
        sepdiff=sepdiffs==0;
    else
        sepdiff=sepdiffs==(str2double(tokens{ii}.length)-str2double(tokens{ii}.separation));
    end

    hasdata(l,w,pol,sepdiff,scn)=true;
    waveguides(l,w,pol,sepdiff,scn).filename=filename{ii};
    waveguides(l,w,pol,sepdiff,scn).length=lengths(l);
    waveguides(l,w,pol,sepdiff,scn).width=widths(w);
    waveguides(l,w,pol,sepdiff,scn).polarization=polarizations(pol);
    waveguides(l,w,pol,sepdiff,scn).sepdiff=sepdiffs(sepdiff);
    waveguides(l,w,pol,sepdiff,scn).scan_number=scn;
    
    waveguides(l,w,pol,sepdiff,scn).data ...
        = load(fullfile(pathname,[filename{ii} '.txt']))/(pixel_time_ms/1000);
    waveguides(l,w,pol,sepdiff,scn).maxval=max(waveguides(l,w,pol,sepdiff,scn).data(:));
    
    lims = load(fullfile(pathname,[filename{ii} '_limits.txt']));    
    waveguides(l,w,pol,sepdiff,scn).ylims = lims([1,2]);
    waveguides(l,w,pol,sepdiff,scn).ystep = lims(3);
    waveguides(l,w,pol,sepdiff,scn).xlims = lims([4,5]);
    waveguides(l,w,pol,sepdiff,scn).xstep = lims(6);
    waveguides(l,w,pol,sepdiff,scn).power = lims(7);
    waveguides(l,w,pol,sepdiff,scn).gain  = lims(8);        
end


return
%% print figures
if do_plots
    for ii=1:length(waveguides)
        if ~isempty(waveguides{ii}.filename)
            x = ((0:size(waveguides{ii}.data,1)-1)*waveguides{ii}.xstep+waveguides{ii}.xlims(1))/voltsperum;
            y = ((0:size(waveguides{ii}.data,2)-1)*waveguides{ii}.ystep+waveguides{ii}.ylims(1))/voltsperum;

            figure(3467);
            clf;
            jcb_contourplot(x,y,waveguides{ii}.data);
            xlabel('X/\mum');
            ylabel('Y/\mum');
            title(strrep(waveguides{ii}.filename,'_',' '));
            
            if ~exist(fullfile(pathname,dirname),'dir')
               mkdir(fullfile(pathname,dirname));
            end
            print('-dpng',fullfile(pathname,dirname,[filename{ii} '_scan']));
        end
    end
end
%%
% waveguides(l,w,pol,sepdiff,scn)
l=lengths>0;
w=widths==4;
pol=polarizations=='v';
sepdiff=sepdiffs==0;
subset=waveguides(l,w,pol,sepdiff,:)

if do_plots
    for w=widths   
        m=log([maxvals{:}]./[power{:}]);
        clf(figure(n+1));

        
        pol=polarizations=='v';
        sepdiff=sepdiffs==0;
        hold on
        cols='rgbcm';
        for scn=1:num_scans
            wg=waveguides(:,widths==w,polarizations=='v',sepdiffs==0,scn);
            scatter(l(logical(strcmp(scan_number,jj).*mask)),m(logical(strcmp(scan_number,jj).*mask)),[cols(str2double(jj)) 'x']);
        end
        legend(unique(scan_number));
        hold off
        xlim([min(l) max(l)]+0.1*range(l).*[-1 1]);
        ylim([min(m) max(m)]+0.1*range(m).*[-1 1]);
        xlabel('waveguide length/\mum');
        ylabel('log((APD counts/s) / photodiode voltage)');
        title(['vertically polarized ' num2str(w) '\mum wide waveguides']);
        print('-dpng',fullfile(pathname,dirname,['Xb' num2str(w) '_v_scatter']));
    end
end

if nargout <1
    clear filename titles data limits maxvals lengths widths polarizations power
end

% f=fopen('power_values.txt','w');
% for ii= 1:length(filename)
%     fprintf(f,[filename{ii} '    ' num2str(power{ii}) '\n']);
% end
% fclose(f);

end